package com.muyan.p2p;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author: YLTDMenglc
 * @Date: 2019/6/25
 * @Time: 14:02
 * @Description:
 */
public class Producer {

    /**
     * mq通讯地址
     */
    private final static String URL = "tcp://localhost:61618";

    /**
     * 队列名称
     */
    private final static String QUEUENAME = "my_queue";

    public static void main(String[] args) throws JMSException {
        // 1.创建ActiveMQFactory
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(URL);
        // 2.创建连接
        Connection cnnection = factory.createConnection();
        // 3.启动连接
        cnnection.start();
        // 4.创建Session 不开启事务,自动签收模式
        Session session = cnnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 5.创建一个目标
        Queue queue = session.createQueue(QUEUENAME);
        // 6.创建生产者
        MessageProducer producer = session.createProducer(queue);
        for (int i = 1; i <= 10; i++) {
            // 7.创建消息
            TextMessage textMessage = session.createTextMessage("消息" + i);
            // 8.发送消息
            producer.send(textMessage);
            System.out.println(textMessage.toString());
        }
        // 9.关闭连接
        cnnection.close();
    }


}
